package com.sogoodsoft.web.crm;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.account.User;
import com.sogoodsoft.entity.crm.OrderDetail;
import com.sogoodsoft.entity.crm.OrderForm;
import com.sogoodsoft.service.common.AbstractService;
import com.sogoodsoft.service.crm.OrderService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.util.InfosysSecurityUtil;
import com.sogoodsoft.web.BaseAction;
import com.sogoodsoft.web.common.Report;

@Results( { 
	@Result(name = "ordercheck", location = "ordercheck.jsp"),
	@Result(name = "MYORDER", location = "myorder.jsp")
})
public class OrderAction extends BaseAction<OrderForm, Long> {

	/**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
	private OrderService service;

	private List<OrderDetail> detail;
	
	private String act;

	@SuppressWarnings("rawtypes")
    @Override
	protected AbstractService getService() {
		return service;
	}

	@Override
	public String save() throws Exception {
		service.save(entity, detail);
		Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils
				.getParameter(DwzMsg.param_navTabId)));
		return null;
	}

	@Override
	public String input() throws Exception {
		detail = entity.getDetail();
		if (detail != null) {
			for (OrderDetail d : detail) {

				String str = "id=" + d.getGoodsid();
				str += " model=" + d.getGoodsmodel();
				str += " unit=" + d.getGoodsunit();
				str += " price=" + d.getGoodsprice();
				str += " count=" + d.getGoodscount();
				logger.debug(str);
			}
		}
		// 生成订单编号
		if (entity.getCode() == null) {
			entity.setCode(String.valueOf(service.getOrderCode()));
		}
		return INPUT;
	}
	
	 @SuppressWarnings("unchecked")
	    @Override
	    public String delete() throws Exception {
		 	entity = service.get(id);
		 	if(entity.getStatusid().compareTo(OrderForm.STATUS.CONFIRMED.getValue())==0){
		 		Struts2Utils.renderJson(DwzMsg.fail("对不起，此定单已通过收款确认，不能删除！"));
		 		return null;
		 	}
	        getService().delete(id);
	        Struts2Utils.renderJson(DwzMsg.success());
	        return null;
	    }
	 
	 public String myOrder() throws Exception{
		  List < PropertyFilter > filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		  User user = InfosysSecurityUtil.getCurrentUser();
		  PropertyFilter prop = new PropertyFilter("EQL_makerid",String.valueOf(user.getId()) );
		  filters.add(prop);
	        // 设置默认排序方式
	        if (!page.isOrderBySetted()) {
	            page.setOrderBy("orderdate");
	            page.setOrder(order);
	        }

	        logger.debug("查询列表");
	        page = getService().findPage(page, filters);
		 return "MYORDER";
	 }

	/**
	 * 查看订单
	 * @return
	 * @throws Exception
	 */
	public String ordercheck() throws Exception{
		prepareModel();
		return "ordercheck";
	}
	
	/**
	 * 报表与打印
	 * @return
	 * @throws Exception
	 */
	public String print()throws Exception{
		prepareModel();		
		
		/**       导出需要设置的属性                **/
//		report.storeName = "订单"+entity.getCode();
//		report.ext = Report.EXT.pdf;
//		report.putID("OID",id); //此句等价于：report.para.put("OID", new BigDecimal(id));
//		report.jasper = "report1.jasper";
		
		/**       打印需要设置的属性                **/
		report.print = true;
		report.putID("OID", id);
		report.jasper = "report1.jasper";
		
		return report.make();
	}
	
	public String pdf()throws Exception{
		prepareModel();	
		report.storeName = "订单"+entity.getCode();
		report.ext = Report.EXT.pdf;
		report.putID("OID",id); //此句等价于：report.para.put("OID", new BigDecimal(id));
		report.jasper = "report1.jasper";
		return report.make();
	}
	
	public String excel()throws Exception{
		prepareModel();	
		report.storeName = "订单"+entity.getCode();
		report.ext = Report.EXT.excel;
		report.putID("OID",id); //此句等价于：report.para.put("OID", new BigDecimal(id));
		report.jasper = "report1.jasper";
		return report.make();
	}
	
	public List<OrderDetail> getDetail() {
		return detail;
	}

	public void setDetail(List<OrderDetail> detail) {
		this.detail = detail;
	}

	public String getAct() {
		return act;
	}

	public void setAct(String act) {
		this.act = act;
	}



}
